package com.ndood.spider.taobaomix.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ndood.core.render.SeleniumDownloader;
import com.ndood.core.render.impl.PhantomJsDriverPool;
import com.ndood.spider.taobaomix.action.TaobaoMixAction;
import com.ndood.spider.taobaomix.dao.TaobaoMixGoodsDao;
import com.ndood.spider.taobaomix.pipeline.TaobaoMixPipeline;
import com.ndood.spider.taobaomix.processor.TaobaoMixPageProcessor;
import com.ndood.spider.tmall.action.TmallAction;

import us.codecraft.webmagic.Spider;

/**
 * 爬虫控制器
 */
@RestController
@RequestMapping("/taobao_mix")
public class TaobaoMixSpiderController {

	@Autowired
	private TaobaoMixPipeline taobaoMixPipeline;

	private AtomicBoolean isOk = new AtomicBoolean(false);
	
	@Autowired
	private TaobaoMixPageProcessor taobaoMixPageProcessor;
	
	/*@Autowired
	private TaobaoMixGoodsDao taobaoMixGoodsDao;*/
	
	static String cat = "kids";
	
	static String[] urls = {
		"https://item.taobao.com/item.htm?spm=a219r.lm895.14.24.30703bd0RVTomD&id=557630666170&ns=1&abbucket=5",
	};
	
	/**
	 * 爬取一个静态文件上的连接
	 * @param cat
	 * @param catPath
	 * @param index
	 * @return
	 * 			"https://item.taobao.com/item.htm?id=559675146082&ali_refid=a3_430585_1006:1107360469:N:%E6%89%8B%E6%9C%BA%E5%A3%B3:cf0b94c3b5c347aaea0a93669dba9ce9&ali_trackid=1_cf0b94c3b5c347aaea0a93669dba9ce9&spm=a230r.1.14.6#detail"
	 * @throws Exception 
	 */
	@RequestMapping("/spider")
	public String spider() throws Exception {
		/*for (int i=0;i<urls.length;i++) {
			urls[i] = urls[i]+"?cat="+cat;
		}*/
		Spider spider = Spider.create(taobaoMixPageProcessor);
		spider.addUrl(urls);
		spider.addPipeline(taobaoMixPipeline);
		spider.setDownloader(new SeleniumDownloader(5000, new PhantomJsDriverPool(), new TaobaoMixAction()));
		spider.thread(1);
		spider.setExitWhenComplete(true);
		spider.start();
		while (!isOk.get()) {
			Thread.sleep(1000);
		}
		isOk.set(false);
		return "ok";
	}
	
	@RequestMapping("/spiderOne")
	public String spiderOne() throws Exception {
		String url = readFile("C:/Users/qilongfei/Desktop/id.txt");
		url = url.trim();
		System.out.println(url);
		String[] urls = new String[]{url};
		/*for (int i=0;i<urls.length;i++) {
			urls[i] = urls[i]+"&cat="+cat;
		}*/
		Spider spider = Spider.create(taobaoMixPageProcessor);
		spider.addUrl(urls);
		spider.addPipeline(taobaoMixPipeline);
		spider.setDownloader(new SeleniumDownloader(5000, new PhantomJsDriverPool(), new TaobaoMixAction()));
		spider.thread(2);
		spider.setExitWhenComplete(true);
		spider.start();
		while (!isOk.get()) {
			Thread.sleep(1000);
		}
		isOk.set(false);
		return "ok";
	}
	
	@RequestMapping("/modify")
	public String modify() throws Exception {
		String path = "C:/Users/Administrator/Desktop/taobaoGoods新需求";
		// Step1: 遍历所有商品文件夹
		File file = new File(path);
		
		// Step2: 读取所有商品文件夹下的描述图片文件
		// Step3: 遍历所有描述图片
		// Step4: 统计描述图片的总张数，文件大小小于4k的图片张数
		return null;
	}
	
	public static List<String> getNotEnoughPaths() {
		List<String> notEnoughPaths = new ArrayList<String>();
		String path = "C:/Users/Administrator/Desktop/taobaoGoods新需求";
		// Step1: 遍历所有商品文件夹
		File file = new File(path);
		for (File sub2File : file.listFiles()) {
			// 
			if(sub2File.isDirectory()){
				for (File sub3File : sub2File.listFiles()) {
					// 商品
					if(sub3File.isDirectory()){
						for (File sub4File : sub3File.listFiles()) {
							// desc
							if(!sub4File.isDirectory()){
								continue;
							}
							int count = sub4File.listFiles().length;
							int smallImageCount = 0;
							if(sub4File.isDirectory()&&sub4File.getName().contains("desc")){
								for (File image : sub4File.listFiles()) {
									long totalSpace = image.length();
									if(totalSpace < 5*1024){
										smallImageCount ++;
									}
								}
							}
							if(smallImageCount>0){
								notEnoughPaths.add(sub3File.getName() + "-"+count+"-"+smallImageCount);
							}
						}
					}
				}
			}
		}
		return notEnoughPaths;
	}
	
	public static void main(String[] args) {
		getNotEnoughPaths();
	}
	
	/**
	 * 关闭爬虫
	 * @return
	 */
	@RequestMapping("/stop")
	public String stop() {
		isOk.set(true);
		return "stoped";
	}
	
	private String readFile(String fileName) {
		File myFile = new File(fileName);
		if (!myFile.exists()) {
			System.err.println("Can't Find " + fileName);
		}
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
			StringBuilder sb = new StringBuilder();
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
			in.close();
			return sb.toString();
		} catch (IOException e) {
			e.getStackTrace();
		}
		return null;
	}
}
